﻿using BW.IBLL;
using BW.IDAL;
using BW.Model;
using System.Drawing;
using System.Security.Cryptography;
using System.Xml.Linq;
using System;
using Dto;
using System.Reflection;

namespace BW.BLL
{
    public class BaseBLL : IBaseBLL
    {
        public BaseBLL(IbaseDAL<UserModel> dal,IbaseDAL<UserTypeModel> dal2)
        {
            Dal = dal;
            Dal2 = dal2;
        }

        public IbaseDAL<UserModel> Dal { get; }
        public IbaseDAL<UserTypeModel> Dal2 { get; }
      
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">对象</param>
        /// <returns>受影响的行数</returns>
        public async Task<int> tianjia(UserModel model)
        {
          
            bool var = Dal.GetAllAsync().Any(x => x.Uname == model.Uname);
            if (var)
            {
                return -1;
            }
            else
            {
                return await Dal.AddAsync(model);
            }
        }
        /// <summary>
        /// 显示+分页+模糊查询+等值查询
        /// </summary>
        /// <param name="index">总页容量</param>
        /// <param name="size">总数据</param>
        /// <param name="uname">姓名</param>
        /// <param name="tid">职业类型</param>
        /// <returns>受影响的行数</returns>
        public MyDto xisnshi(int index,int size,string? uname,int? tid)
        {
            var lml = Dal.GetAllAsync();
            var lyp = Dal2.GetAllAsync();
            var quer = from a in lml
                       join b in lyp on a.Tid equals b.Tid
                       select new
                       {
                           a.Uid,
                           a.Uname,
                           a.Set,
                           a.ZJH,
                           a.Barder,
                           a.Adress,
                           a.Zid,
                           a.Phone,
                           a.Sarter,
                           a.Tid,
                           b.Tname
                       };
            //查询
            if (!string.IsNullOrEmpty(uname))
            {
                quer = quer.Where(a => a.Uname.Contains(uname));
            }
            if(tid<0)
            {
                quer = quer.Where(x => x.Tid == tid);
            }
            int c = quer.Count();
            var d = quer.OrderBy(x => x.Uid).Skip((index - 1) * size).Take(size).ToList();
            return new MyDto { Count = c, Members = d };
        }
        /// <summary>
        /// 单个上架下架
        /// </summary>
        /// <param name="id">对象</param>
        /// <returns>受影响的行数</returns>
        public Task<int> UpdateStart(int id)
        {

            var pro = Dal.GetAllAsync().FirstOrDefault(x => x.Uid == id);
            pro.Sarter = pro.Sarter == 0 ? 1 : 0; //下架变上架，上架变下架
            return Dal.UpdateAsync(pro);
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<UserTypeModel> bang()
        {
            return Dal2.GetAllAsync().ToList();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="employee"></param>
        /// <returns></returns>
        public async Task<int> UpdateEmployee(UserModel employee)
        {
            //手机唯一性验证
            bool exitis = Dal.GetAllAsync().Any(x => x.Uname == employee.Uname && x.Uid != employee.Uid);
            if (exitis)
            {
                return -1;
            }
            else
            {
                return await Dal.UpdateAsync(employee);
            }
        }
        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserModel GetEmployeeById(int id)
        {
            var emp = Dal.GetAllAsync().FirstOrDefault(x => x.Uid == id);
            return emp;
        }
    }
}
